package com.sai.jsai.module.system.service.impl;

import com.sai.jsai.common.starter.mybatisplus.service.impl.BaseServiceImpl;
import com.sai.jsai.module.system.api.dto.SysMenuTreeDTO;
import com.sai.jsai.module.system.api.dto.SysRoleDTO;
import com.sai.jsai.module.system.api.dto.SysUserRolePermissionDTO;
import com.sai.jsai.module.system.convert.SysUserConvert;
import com.sai.jsai.module.system.dal.entity.SysUser;
import com.sai.jsai.module.system.dal.mapper.SysUserMapper;
import com.sai.jsai.module.system.service.SysMenuService;
import com.sai.jsai.module.system.service.SysRoleService;
import com.sai.jsai.module.system.service.SysUserRoleService;
import com.sai.jsai.module.system.service.SysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author jisai
 * @version 0.0.1
 * @Description 系统用户 服务实现类
 * @createTime 2024/09/24
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    SysUserRoleService sysUserRoleService;
    @Resource
    SysRoleService sysRoleService;
    @Resource
    SysMenuService sysMenuService;

    /**
     * 根据用户名获取系统用户
     *
     * @param username 用户名
     * @return 系统用户
     */
    @Override
    public SysUser getByUsername(String username) {
        return this.baseMapper.selectByUsername(username);
    }

    /**
     * 根据用户名获取用户角色权限信息
     *
     * @param username 用户名
     * @return 用户角色权限信息
     */
    @Override
    public SysUserRolePermissionDTO getUserRolePermissionByUsername(String username) {
        SysUser sysUser = this.baseMapper.selectByUsername(username);
        List<Long> roleIds =  sysUserRoleService.getRoleIdsByUserId(sysUser.getId());
        List<SysRoleDTO> sysRoles = sysRoleService.listByIds(roleIds);
        List<SysMenuTreeDTO> menuTreeList = sysMenuService.getMenuTreeByRoleIds(roleIds);

        SysUserRolePermissionDTO sysUserRolePermission = SysUserConvert.INSTANCE.convertToSysUserRolePermissionDTO(sysUser);
        sysUserRolePermission.setRoles(sysRoles);
        sysUserRolePermission.setMenus(menuTreeList);
        return sysUserRolePermission;
    }

}
